import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.undo.CannotRedoException;

public class MenuLogo extends JMenuBar implements ActionListener{
	
	//on recupere les elements de l'interface pour pouvoir les modifier avec les menu; 
	private Logo logo;
	private JInternalFrame JIFedit,JIFexec,JIFconsol;
	private Editeur edit;
	private Execution exec;
	private Console consol;
	
	private JCheckBoxMenuItem JCBMediteur,JCBMexecution,JCBMconsole,JCBtelecommande;
	private JMenuItem itemNouveau,itemOpen,itemSave,itemSaveAs,itemPrint;
	private JMenuItem itemCopy,itemCut,itemPaste,itemInfo,itemVersion;
	public JMenuItem itemUndo,itemRedo,itemSearch;
	
	private String selection="";
	
	public MenuLogo(Logo logo){
		super();
		this.logo = logo;
		consol = logo.getConsol();
		edit = logo.getEdit();
		exec = logo.getExec();
		JIFconsol = logo.getJIFConsol();
		JIFedit = logo.getJIFEdit();
		JIFexec = logo.getJIFExec();		
		
		JMenu menuFichier = new JMenu("Fichier");
		JMenu menuEdition = new JMenu("Edition");
		JMenu menuAffichage = new JMenu("Affichage");
		JMenu menuAide = new JMenu("Aide");
		
		itemNouveau = new JMenuItem("Nouveau");	
		itemNouveau.setIcon(new ImageIcon(Logo.Inew));
		itemOpen = new JMenuItem("Ouvrir...");			    
		itemOpen.setIcon(new ImageIcon(Logo.Iopen));
		itemSave = new JMenuItem("Enregistrer");
		itemSave.setIcon(new ImageIcon(Logo.Isave));
		itemSaveAs = new JMenuItem("Enregistrer sous...");
		itemSaveAs.setIcon(new ImageIcon(Logo.Isave));
		itemPrint = new JMenuItem("Imprimer...");
		itemPrint.setIcon(new ImageIcon(Logo.Iprint));
		itemNouveau.addActionListener(this);
		itemOpen.addActionListener(this);
		itemSave.addActionListener(this);		
		itemSaveAs.addActionListener(this);
		itemPrint.addActionListener(this);
		
		
		itemUndo = new JMenuItem("Annuler");
		itemUndo.setIcon(new ImageIcon(Logo.Iundo));
		itemRedo = new JMenuItem("Refaire");
		itemRedo.setIcon(new ImageIcon(Logo.Iredo));
		itemCopy = new JMenuItem("Copier");
		itemCopy.setIcon(new ImageIcon(Logo.Icopy));
		itemCut = new JMenuItem("Couper");
		itemCut.setIcon(new ImageIcon(Logo.Icut));
		itemPaste = new JMenuItem("Coller");
		itemPaste.setIcon(new ImageIcon(Logo.Ipaste));
		itemSearch = new JMenuItem("Rechercher");
		itemSearch.setIcon(new ImageIcon(Logo.ISearch));
		itemUndo.addActionListener(this);
		itemUndo.setEnabled(false);
		itemRedo.addActionListener(this);
		itemRedo.setEnabled(false);
		itemCopy.addActionListener(this);
		itemCut.addActionListener(this);
		itemPaste.addActionListener(this);
		itemSearch.addActionListener(this);
		
		
		JCBMediteur = new JCheckBoxMenuItem("Editeur",true);
		JCBMediteur.setIcon(new ImageIcon(Logo.Iedit));
		JCBMexecution = new JCheckBoxMenuItem("Execution",true);
		JCBMexecution.setIcon(new ImageIcon(Logo.Iexec));
		JCBMconsole = new JCheckBoxMenuItem("Console",true);
		JCBMconsole.setIcon(new ImageIcon(Logo.Iconsol));
		JCBtelecommande = new JCheckBoxMenuItem("Telecommande",false);
		JCBtelecommande.setIcon(new ImageIcon(Logo.Itelecommande));
		JCBMediteur.addActionListener(this);
		JCBMexecution.addActionListener(this);
		JCBMconsole.addActionListener(this);
		JCBtelecommande.addActionListener(this);
		
		itemInfo = new JMenuItem("Qu'est ce que le LOGO ?");
		itemInfo.setIcon(new ImageIcon(Logo.Iabout));
		itemVersion = new JMenuItem("Version");
		itemVersion.setIcon(new ImageIcon(Logo.Iversion));
		itemInfo.addActionListener(this);
		itemVersion.addActionListener(this);		
			    
		this.add(menuFichier);
		this.add(menuEdition);
		this.add(menuAffichage);
		this.add(menuAide);	       	    
		
		menuFichier.add(itemNouveau);
		menuFichier.addSeparator();
		menuFichier.add(itemOpen);
		menuFichier.add(itemSave);
		menuFichier.add(itemSaveAs);
		menuFichier.addSeparator();
		menuFichier.add(itemPrint);
		
		menuEdition.add(itemUndo);
		menuEdition.add(itemRedo);
		menuEdition.addSeparator();
		menuEdition.add(itemCopy);
		menuEdition.add(itemCut);
		menuEdition.add(itemPaste);
		menuEdition.addSeparator();
		menuEdition.add(itemSearch);
		
		menuAffichage.add(JCBMediteur);
		menuAffichage.add(JCBMexecution);
		menuAffichage.add(JCBMconsole);
		menuAffichage.addSeparator();
		menuAffichage.add(JCBtelecommande);
		
		menuAide.add(itemInfo);
		menuAide.add(itemVersion);	
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == JCBMediteur) {JIFedit.setVisible(!JIFedit.isVisible());}
		if (e.getSource() == JCBMexecution) {JIFexec.setVisible(!JIFexec.isVisible());}
		if (e.getSource() == JCBMconsole) {JIFconsol.setVisible(!JIFconsol.isVisible());}
		if (e.getSource() == JCBtelecommande) {logo.setTelecommande();}
		if (e.getSource() == itemNouveau) 	{logo.nouveau();}
		if (e.getSource() == itemOpen) {logo.ouvrir();}
		if (e.getSource() == itemSaveAs) {logo.enregistrerSous();}
		if (e.getSource() == itemSave) {logo.enregistrer();}
		if (e.getSource() == itemVersion) 
		{
			JOptionPane.showMessageDialog(this,"Interpreteur LOGO : version 1.3 \n\n" +
					"Auteurs : \n" +
					"\t - Delfour Kevin \n" +
					"\t - Allier Simon \n" +
					"\t - Lalluque Renaud \n" +
					"\t - Grollemund Naitan \n",
					"Interpreteur Logo",
					JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == itemInfo)
		{			
			URL recup;
			try {
				recup = new URL(logo.getDocumentBase(), "http://www.algo.be/logo1/pdf/man-MSWLogo-fr.pdf");
				logo.getAppletContext().showDocument(recup, "_blank");
			} catch (MalformedURLException netException) {
				JOptionPane.showMessageDialog(this,
						"Impossible d'acceder a la documentation, verifier votre acces internet",
						"Erreur",
						JOptionPane.ERROR_MESSAGE);													
			}		
		}
		if(e.getSource()==itemCopy){
			selection=edit.getJTexteArea().getSelectedText();
		}		
		
		if(e.getSource()==itemCut){
			selection=edit.getJTexteArea().getSelectedText();
			edit.getJTexteArea().replaceRange("", edit.getJTexteArea().getSelectionStart(), edit.getJTexteArea().getSelectionEnd());
		}
		
		if(e.getSource()==itemPaste){
			edit.getJTexteArea().insert(selection,edit.getJTexteArea().getCaretPosition());
		}	
		if(e.getSource()==itemSearch){
			edit.getJTexteLogo().lancerRecherche();
		}
		if(e.getSource()==itemPrint){
			new ImpressionLogo(logo).setVisible(true);
		}
		if(e.getSource()==itemUndo){
			try {
		          logo.getEdit().getJTexteLogo().undoManager.undo();
		        }
			catch (CannotRedoException cre) {
		          cre.printStackTrace();
		        }
			logo.updateButtonsUndoRedo();
		}
		if(e.getSource()==itemRedo){
			try {
				logo.getEdit().getJTexteLogo().undoManager.redo();
	        } 
			catch (CannotRedoException cre) {
	          cre.printStackTrace();
	        }
			logo.updateButtonsUndoRedo();
		}
		
	}
	
	


}
